package org.wisdom.controller;

import org.apache.coyote.BadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wisdom.request.LoginRequest;
import org.wisdom.request.RegisterRequest;
import org.wisdom.response.Result;
import org.wisdom.service.AuthService;

@RestController
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("/auth/login")
    public Result<String> login(@Validated @RequestBody LoginRequest loginRequest) throws BadRequestException {
        return Result.successWithMsg("登录成功！", authService.login(loginRequest));
    }

    @PostMapping("/auth/register")
    public Result<?> register(@Validated @RequestBody RegisterRequest registerRequest) throws BadRequestException {
        authService.register(registerRequest);
        return Result.successWithMsg("注册成功！");
    }

    /**
     * <h1> 校验 token 是否合法 </h1>
     * 实际上到达控制层这里, 已经通过过滤器校验了 token 合法性, 因此这里直接返回成功即可。
     */
    @GetMapping("/check-token")
    public Result<?> checkToken() {
        return Result.success();
    }

}
